<?php
class ModelArticleFeedback extends BaseModel {

    protected function table(){
        return "wp_comments";
    }

    protected function primary_key_name(){
        return "comment_ID";
    }

    public function get_province_list(){
        return  $this->db->query("select * from country order by country_id")->rows;
    }
    public function get_city_list($id){
        return  $this->db->query("select * from zone where country_id={$id} order by zone_id")->rows;
    }

    public function comment($sender_name,$customer_id,$sender_id,$message) {

        $sql = "insert into wp_comments 
		set comment_post_ID=0,
		comment_author='{$sender_name}',
		comment_parent=0,
		comment_date=CURRENT_TIMESTAMP,
		comment_date_gmt=CURRENT_TIMESTAMP, 
		comment_approved=1,
		comment_type={$customer_id},
		user_id={$sender_id},
		comment_content='{$message}'";


        $query = $this->db->query($sql);
    }



    public function getList($data = array()) {
        $sql = "select a.comment_date,b.firstname,b.customer_id as ID from(
SELECT
	comment_type as customer_id, max(comment_date) as comment_date
FROM
	wp_comments
WHERE
	comment_post_ID = 0
AND comment_type IS NOT NULL
GROUP BY
	comment_type
) a left join customer b on a.customer_id = b.customer_id
order by comment_date desc";


        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        $ret = $query->rows;


        return $ret;
    }

    public function getCount() {
        $sql = "select a.comment_date,b.firstname from(
SELECT
	comment_type as customer_id, max(comment_date) as comment_date
FROM
	wp_comments
WHERE
	comment_post_ID = 0
AND comment_type IS NOT NULL
GROUP BY
	comment_type
) a left join customer b on a.customer_id = b.customer_id";

        $query = $this->db->query("SELECT COUNT(*) AS total FROM (" .$sql . ") a" );

        return $query->row['total'];
    }


    public function getItem($id) {
        $query = $this->db->query("select a.*,b.image from wp_comments a left join customer b on a.user_id = b.customer_id where comment_type = {$id} order by a.comment_ID desc" );
        return $query->rows;
    }




}